package com.yanqu.road.dao.impl.activity.summerquiz;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.summerquiz.CrossSummerQuizData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossSummerQuizDataDaoImpl extends TempDataObjectDao<CrossSummerQuizData> {

    public static String getCrossSummerQuizDataSql = "select * from `t_c_summer_quiz_data` where `activityId`=?";
    public Map<Integer, Map<Integer, CrossSummerQuizData>> getCrossSummerQuizData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, CrossSummerQuizData>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossSummerQuizDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossSummerQuizData quizData = parse(rs);
                if(!map.containsKey(quizData.getGameActivityId())){
                    map.put(quizData.getGameActivityId(), new ConcurrentHashMap<>());
                }
                map.get(quizData.getGameActivityId()).put(quizData.getQuizConfigId(), quizData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCrossSummerQuizDataSql = "update `t_c_summer_quiz_data` set oddsYes=?,oddsNo=?,yesCount=?,noCount=?,yesSum=?,noSum=?,settle=?,lastOddsTime=? where activityId=? and quizConfigId=? and gameActivityId=?";
    @Override
    protected boolean updateData(CrossSummerQuizData crossSummerQuizData) {
        crossSummerQuizData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerQuizData.getOddsYes()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerQuizData.getOddsNo()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerQuizData.getYesCount()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerQuizData.getNoCount()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerQuizData.getYesSum()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerQuizData.getNoSum()));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossSummerQuizData.isSettle()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerQuizData.getLastOddsTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerQuizData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerQuizData.getQuizConfigId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerQuizData.getGameActivityId()));
        if (getDb().executeNoneQuery(updateCrossSummerQuizDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossSummerQuizDataSql = "insert into `t_c_summer_quiz_data`(activityId,quizConfigId,gameActivityId,oddsYes,oddsNo,yesCount,noCount,yesSum,noSum,settle,lastOddsTime) values (?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossSummerQuizData crossSummerQuizData) {
        crossSummerQuizData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerQuizData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerQuizData.getQuizConfigId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerQuizData.getGameActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerQuizData.getOddsYes()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerQuizData.getOddsNo()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerQuizData.getYesCount()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerQuizData.getNoCount()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerQuizData.getYesSum()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerQuizData.getNoSum()));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossSummerQuizData.isSettle()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerQuizData.getLastOddsTime()));
        if (getDb().executeNoneQuery(insertCrossSummerQuizDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossSummerQuizData parseData(ResultSet rs) throws SQLException {
        CrossSummerQuizData crossSummerQuizData = new CrossSummerQuizData();
        crossSummerQuizData.setActivityId(rs.getInt("activityId"));
        crossSummerQuizData.setQuizConfigId(rs.getInt("quizConfigId"));
        crossSummerQuizData.setGameActivityId(rs.getInt("gameActivityId"));
        crossSummerQuizData.setOddsYes(rs.getInt("oddsYes"));
        crossSummerQuizData.setOddsNo(rs.getInt("oddsNo"));
        crossSummerQuizData.setYesCount(rs.getInt("yesCount"));
        crossSummerQuizData.setNoCount(rs.getInt("noCount"));
        crossSummerQuizData.setYesSum(rs.getLong("yesSum"));
        crossSummerQuizData.setNoSum(rs.getLong("noSum"));
        crossSummerQuizData.setSettle(rs.getBoolean("settle"));
        crossSummerQuizData.setLastOddsTime(rs.getLong("lastOddsTime"));

        return crossSummerQuizData;
    }
}
